package com.goktech.olala.core.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.goktech.olala.core.service.IIndexService;
import com.goktech.olala.server.dao.goods.*;
import com.goktech.olala.server.pojo.goods.*;
import com.goktech.olala.utils.RMBUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Iterator;
import java.util.List;

/**
 * @Classname IndexServiceImpl
 * @Description TODO
 * @Date 2020/12/26 20:29
 * @Created by wjs
 */
@Service
public class IndexServiceImpl implements IIndexService {
    @Autowired
    ArticleMapper articleMapper;

    @Autowired
    ActivitMapper activitMapper;

    @Autowired
    AdversMapper adversMapper;

    @Autowired
    CategoryMapper categoryMapper;

    @Autowired
    GoodsBrandMapper goodsBrandMapper;

    @Autowired
    GoodsMapper goodsMapper;

    @Autowired
    GoodsReportMapper goodsReportMapper;

    @Autowired
    PictureMapper pictureMapper;

    @Override
    public List<Article> selectAllArticle() {
        return articleMapper.selectAll();
    }

    @Override
    public List<Activit> selectAllActivit() {
        return activitMapper.selectAll();
    }

    @Override
    public List<Advers> selectAllAdvers() {
        return adversMapper.selectAll();
    }

    @Override
    public List<ParentCategory> selectAllCategory() {
        //首先查询全部商品一级分类
        List<ParentCategory> parentCategories = categoryMapper.selectByParentId("-1");

        //循环一级分类，查找第二级分类
        for (int i = 0; i < parentCategories.size(); i++) {
            //创建一个《实力商家集合》
            List<GoodsBrand> goodsBrands = null;

            //查询本级别内部的实力商家并放入《实力商家集合》
            goodsBrands = addGoodsBrands(goodsBrands, parentCategories.get(i));

            //查询该一级分类内的二级分类
            List<ParentCategory> parentCategories2 = selectByParentId(parentCategories.get(i));
            //循环二级分类，查找三级分类
            for (int j = 0; j < parentCategories2.size(); j++) {
                //查询本级别内部的实力商家并放入《实力商家集合》
                goodsBrands = addGoodsBrands(goodsBrands, parentCategories2.get(j));

                //查询该二级分类内的三级分类
                List<ParentCategory> parentCategories3 = selectByParentId(parentCategories2.get(j));
                //使用迭代器进行三级分类内部数据有效性检查
                Iterator<ParentCategory> iterator = parentCategories3.iterator();
                //循环
                while (iterator.hasNext()) {
                    ParentCategory next = iterator.next();
                    //查询本级别内部的实力商家并放入《实力商家集合》
                    goodsBrands = addGoodsBrands(goodsBrands, next);
                }
                //三级分类集合放入对应二级分类内部
                parentCategories2.get(j).setChildList(parentCategories3);

            }
            //二级分类集合放入对应一级分类内部
            parentCategories.get(i).setChildList(parentCategories2);
            //一级分类中加入本级及以下级别的实力商家
            parentCategories.get(i).setBrandList(goodsBrands);
        }
        return parentCategories;
    }

    @Override
    public List<GoodsBrand> searchBrand(String keywords) {
        return goodsBrandMapper.selectByName(keywords);
    }

    @Override
    public List<Category> searchCategory(String keywords) {
        return categoryMapper.selectByName(keywords);
    }

    @Override
    public List<GoodsVO> searchGoods(String keywords,Integer page) {
        if (page!=null){
            PageHelper.startPage(page, 12);
        }else{
            PageHelper.startPage(1, 12);
        }
        List<GoodsVO> vos = goodsMapper.keywordSelect(keywords);
        for (GoodsVO temp : vos) {
            try {
                temp.setShopPrice1(RMBUtil.changeF2Y(Long.valueOf(temp.getShopPrice())));
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return vos;
    }

    @Override
    public List<GoodsReport> searchHotSale(String keywords) {
        List<GoodsReport> goodsReports = goodsReportMapper.selectAll();
        return goodsReports;
    }

    @Override
    public GoodsVO2 queryGoodsDetail(String goodsId) {
        GoodsVO2 goodsVO2 = new GoodsVO2();

        Goods goods = goodsMapper.selectByPrimaryKey(goodsId);

        //将父类的值给子类
        BeanUtils.copyProperties(goods,goodsVO2);
        //价格转换
        try{
            goodsVO2.setCostPrice(RMBUtil.changeF2Y(Long.valueOf(goodsVO2.getCostPrice())));
            goodsVO2.setMarketPrice(RMBUtil.changeF2Y(Long.valueOf(goodsVO2.getMarketPrice())));
            goodsVO2.setPromotePrice(RMBUtil.changeF2Y(Long.valueOf(goodsVO2.getPromotePrice())));
            goodsVO2.setShopPrice(RMBUtil.changeF2Y(Long.valueOf(goodsVO2.getShopPrice())));
        }catch (Exception e){

        }

        //查询图片
        List<Picture> pictures = pictureMapper.selectByRelationId(goodsId);
        goodsVO2.setPictureVos(pictures);

        return goodsVO2;
    }

    /**
     * 传入分类级别查询下一级别的分类
     *
     * @param parentCategory
     * @return
     */
    private List<ParentCategory> selectByParentId(ParentCategory parentCategory) {
        return categoryMapper.selectByParentId(parentCategory.getCategoryId().toString());
    }

    /**
     * 传入《原实力商家集合》和本级分类
     * 通过本级分类查询本级《实力商家集合》
     * 然后将两个集合拼接到一起
     *
     * @param beforeList
     * @param parentCategory
     * @return
     */
    private List<GoodsBrand> addGoodsBrands(List<GoodsBrand> beforeList, ParentCategory parentCategory) {
        List<GoodsBrand> addList = goodsBrandMapper.selectByCategoryId(parentCategory.getCategoryId());
        if (beforeList != null) {
            beforeList.addAll(addList);
        } else {
            beforeList = addList;
        }
        return beforeList;
    }


}
